var express = require('express');
var router = express.Router();
var session = require("express-session");
router.use(session({
    secret:"shiji",
    resave:false,
    saveUninitialized:true
}));
var md5 = require("md5");
var userDAO = require("../bin/db/userDAO.js");
var strRandom = require("../bin/utils/strRandom.js");

var multer = require("multer");
var picture=[];

var storage = multer.diskStorage({
    destination:function(req,file,cb){
        cb(null,"./public/imgs/avatars")
    },
    filename:function(req,file,cb){
      var ind = file.originalname.lastIndexOf(".");
      var suffix = file.originalname.substr(ind);
      var name = req.session.user._id+suffix;
      req.avatar = name;
      cb(null,name)
    }
});
var upload = multer({storage:storage});

//验证码模块
var ccap = require("ccap");
var captcha = ccap({
  width:190,
	height:70,
	offset:30,
	quality:100,
	fontsize:57,
});

//生成验证码的接口
router.get("/captcha",(req,res)=>{
  var arr = captcha.get();
  req.session.captcha = arr[0];
  res.set("Content-Type","image/jpg");
  res.send(arr[1]);
});

// 注册
router.post("/regist",(req,res)=>{
  console.log(1111)
  if(req.body.captcha.toUpperCase()!=req.session.captcha){
    res.json({err:1,msg:"验证码错误"});
  }else{
    var reg = /^\w{2,12}$/;
    if(!reg.test(req.body.account)){
      res.json({err:1,msg:"账号格式错误"});
      return;
    }   
    if(!req.body.psw){
      res.json({err:1,msg:"密码不能为空"});
        return;
    }
    if((req.body.psw)!=(req.body.confirm)){
      res.json({err:1,msg:"两次密码输入不一致"});
        return;
    }
    userDAO.findOne({account:req.body.account})
    .then(data=>{
      if(data){
        res.json({err:1,msg:"此账号已被占用"});
      }else{
        req.body.psw = md5(req.body.psw);
        var u = new userDAO(req.body);
        u.save(function(err){
          res.json({err:0,msg:"注册成功"})
        }) 
      }
    })
  }
  
})

//判断验证码的接口
router.post("/check",(req,res)=>{
  setTimeout(function() {
    if(req.body.captcha.toUpperCase()==req.session.captcha){
      res.json({err:0});
    }else{
      res.json({err:1});
    }
  }, 1000);
});
//判断是否已登录的接口
router.post("/islogin",(req,res)=>{
  if(req.session.user){
    res.json({err:0,userinfo:req.session.user});
  }else{
    res.json({err:1,msg:"请先登录"});
  }
});

// 登录
router.post("/login",(req,res)=>{
  // console.log(111);
  userDAO.findOne({account:req.body.account})
  .then(data=>{
    if(!data){
      res.json({err:1,msg:"此用户不存在"});
    }else{
      // console.log(222)
      if(md5(req.body.psw)==data.psw){
        // console.log(333)
        req.session.user = data;
        res.json({err:0,msg:"登录成功"});
      }else{
        console.log(444)
        res.json({err:0,msg:"账号或密码错误"});
      }
    }
  })
})

// 退出登录
router.post("/loginout",(req,res)=>{
  req.session.user ="";
  res.json({err:0,msg:"退出成功"});
})

var fs = require("fs");
//上传头像接口
router.post("/upload",function(req,res,next){
    if(!req.session.user){
        res.send("请先登录");
    }else{
        next();
    }
});

router.post("/upload",upload.single("avatar"),function(req,res){
    //删除以前的头像图片
    // fs.unlinkSync("./public/avatars/"+req.session.user.avatar);
    //修改session中用户信息的头像
    req.session.user.avatar = req.avatar;
    //把头像的修改存入数据库
    userDAO.update({
        _id:req.session.user._id
    },{
        $set:{avatar:req.avatar}
    })
    .then(function(){
        // console.log(1213)
        res.json({err:0,msg:"上传成功"});
    });
});

module.exports = router;
